﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using System;
using QuantBox;
using SmartQuant;

namespace QdpDemo
{
    public class DemoScenario : Scenario
    {
        public DemoScenario(Framework framework) : base(framework)
        {
        }

        private void UpdateInstrument()
        {
            var list = new List<Instrument>();
            //更新上市的期货和期权合约
            list.AddRange(InstrumentManager.GetMainFutures());
            if (list.Count > 0)
            {
                InstrumentManager.Clear();
                foreach (var item in list)
                {
                    if (Regex.IsMatch(item.Symbol, @"[a-zA-Z]+[89]+"))
                    {
                        continue;
                    }
                    InstrumentManager.Add(item);
                }
                InstrumentManager.Server.Flush();
            }
        }

        public override void Run()
        {
            XProvider.BacktestInit();
            StrategyManager.Mode = StrategyMode.Live;
            InstrumentManager.Load();
            if (InstrumentManager.Instruments.Count == 0)
            {
                UpdateInstrument();
            }

            strategy = new Strategy(framework, "QdpTest");

            var strategy1 = new TradingTestStrategy(framework, "test");
            strategy.AddStrategy(strategy1);

            var instrument = InstrumentManager["i2305"];
            //inst.DataProvider = ProviderManager.GetCtpData();
            var provider = ProviderManager.NewQdp("QDP仿真", "80010126", "123456");
            provider.QueryTradingDataAfterTrade = false;
            provider.DiscardOutOfTimeRange = true;

            var dp = ProviderManager.NewCtpData("银河电信",string.Empty,string.Empty);
            instrument.DataProvider = dp;
            instrument.ExecutionProvider = provider;
            strategy1.AddInstrument(instrument);
            new StrategyServer(this).Init();
            StartLive();
        }
    }
}